import csv
import json
import requests
import os

def pic_get():
    csv_name = input('请输入要读取的文件 \n')
    file = './' + csv_name + '.csv'
    count = 0
    # 创建文件夹
    os.mkdir(csv_name)
    with open(file, encoding='utf-8-sig') as f:
        for row in csv.reader(f, skipinitialspace=True):
            if(count > 0):
                # print(row)
                # 提取封面url
                url = row[5]
                json_str = url.replace("'", '"')
                # json.loads() ,要求json串格式中必须的双引号！！转换为字典
                json_dict = json.loads(json_str)
                print(json_dict['url'])
                req = requests.get(json_dict['url'])
                # 写入文件
                open(csv_name+'/'+str(count)+'.jpg','wb').write(req.content)
            count += 1
    f.close()


if __name__ == '__main__':
    pic_get()
